<?php
namespace Sys\Controller;
use Common\Controller\BaseController;
class DictController extends BaseController{
    public function index(){
        $m=M('dict');
        if($val=I('get.field')){
            $where=[
                'field'=>$val
            ];
        }else{
            $where=[];
        }
        $list=$m->field('field')->group('field')->select();
        $this->assign('search',$list);
        $res=$m->where( $where)->page(I('get.p'),5)->select();
        $this->assign("dict",$res);

        $count = $m->count();
        $Page = new \Think\Page($count,5);
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $show = $Page->show();
        $this->assign('page',$show);


        $this->display();



    }
    public function doEditDict(){
        $m =M('dict');//D(')
        if($m->create()){
            $m->save();
            $res = [
                'msg'=>'编辑成功',
                'error'=>0,
                'data'=>I('post.')
            ];
        }else{
            $res = [
                'msg'=>"编辑失败",
                'error'=>1,

            ];
        }
        $this->ajaxReturn($res);

    }
    public function doDelDict(){
        $where=I('post.ids');
        $m=M('dict');
        if($m->delete($where)){
            $res=[
                'msg'=>'删除成功',
                'error'=>0
            ];
        }else{
            $res=[
                'msg'=>'删除失败',
                'error'=>1
            ];
        }
        $this->ajaxReturn($res);
    }
    public function addDict(){
        $res=M('dict')->select();
        $this->assign('dicts',$res);
        $this->display();
    }
    public function doAddDict(){
        $m = D('dict');
        if(!$m->create($_POST)){
            exit($m->getError());
        }
        if($m->add()){
            $this->success('添加成功',U('./Sys/Dict/index'));
        }else{
            echo $m->getError();
        }

    }

}
